<?php
class Administrator_SellController extends Zend_Controller_Action
{
	public function init()
	{
		$member=new CMember();
		if(!$member->isUser($this->_request->getModuleName(),$this->_request->getControllerName())){	
			$this->_redirect(MY_URL.'/administrator-auth-login');
		}
		
	}
	
	public function preDispatch()
	{
		$this->getHelper ('layout')->setLayout('framelayout');
		$this->view->addHelperPath(MY_ROOT.'/vhelpers');
		Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');
	}
	
	public function indexAction()
	{
		$shareModel = new CPyramidMemberShare();
		$page = $this->getRequest()->getParam('page', 1);
		$pageNum = 10;
		
		$pageCount = $shareModel->getPageCountVisit($pageNum);
		$shareList = $shareModel->getAllVisit($page, $pageNum);
		
		$statistics = new CStatistics();        
        
        $param = 'administrator-sell-index?';
        $pageLink = $statistics->orderPage($pageCount, $pageNum, $page, $param);
        
        $item = new CItem();
        
        $this->view->shareCount = $shareModel->getShareCount();
        $this->view->toShareCount = $shareModel->getToShareCount();
        $this->view->pageLink = $pageLink;
        $this->view->shareList = $shareList;
        $this->view->item = $item;
	}
	
	public function moreAction()
	{
		$currentUserId = $this->getRequest()->getParam('id');	
		$this->view->currentUserId = $currentUserId;
		$memberModel = new CPyramidMember();
		
		//分享信息
		$shareModel = new CPyramidMemberShare();
		
		$myShareCount = $shareModel->getMyShareCount($currentUserId);
		$friendsShareCount = $shareModel->getFriendsShareCount($currentUserId);
		
		//访问信息
		$visitModel = new CPyramidMemberVisit();
				
		$friendsVisitCount = $visitModel->getFriendsVisitCount($currentUserId);
		$interestedFriendsCount = $friendsVisitCount;
		
		//购买信息
		$buyModel = new CPyramidMemberBuy();		
		$myBuyAmount = $buyModel->getMyBuyAmount($currentUserId);
		$friendsBuyAmount = $buyModel->getFriendBuyAmount($currentUserId);
		$buyFriends = $buyModel->getCountBuyFriend($currentUserId);
		
		//佣金信息
		$accountDetailModel = new CPyramidAccountDetail();
		$profit = $accountDetailModel->getMyAmount($currentUserId);
		
		//现金账户
		$accountModel = new CPyramidMemberAccount();
		$cash = $accountModel->getMyCash($currentUserId);
		$rank = $accountModel->getRank($currentUserId);
	
		$this->view->myShareCount = $myShareCount;
		$this->view->friendsShareCount = $friendsShareCount;
		
		$this->view->friendsVisitCount = $friendsVisitCount;
		$this->view->interestedFriendsCount = $interestedFriendsCount;
		
		$this->view->myBuyAmount = $myBuyAmount;
		$this->view->friendsBuyAmount = $friendsBuyAmount;
		$this->view->buyFriends = $buyFriends;
		$this->view->profit = $profit;
		$this->view->rank = $rank;
		
		$this->view->cash = $cash;
		
		$shareModel = new CPyramidMemberShare();
		$page = $this->getRequest()->getParam('page', 1);
		$pageNum = 10;
		
		$pageCount = $shareModel->getPageCount($pageNum, $currentUserId);
		$shareList = $shareModel->getAll($page, $pageNum, $currentUserId);
		
		$statistics = new CStatistics();        
        
        $param = 'administrator-sell-more?id=' . $currentUserId . '&';
        $pageLink = $statistics->orderPage($pageCount, $pageNum, $page, $param);
        
        $item = new CItem();
        
        $this->view->pageLink = $pageLink;
        $this->view->shareList = $shareList;
        $this->view->item = $item;
	}
	
	public function cashAction()
	{
		$cashModel = new CPyramidAccountCrash();
		$page = $this->getRequest()->getParam('page', 1);
		$pageNum = 10;
		
		$type = $this->getRequest()->getParam('type', 5);
		
		$cashList = $cashModel->getAll($page, $pageNum, $type);
		$pageCount = $cashModel->getPageCount($pageNum, $type);
		$statistics = new CStatistics();        
        
        $param = 'administrator-sell-cash?type=' . $type . '&';
        $pageLink = $statistics->orderPage($pageCount, $pageNum, $page, $param);
        
        $this->view->pageLink = $pageLink;
        $this->view->cashList = $cashList;
        
        $this->view->type = $type;
        
	}
	
	public function casheditAction()
	{
		$cashModel = new CPyramidAccountCrash();
		$id = $this->getRequest()->getParam('id', 1);
		$cash = $cashModel->find($id)->current();
		$this->view->id = $id;
		$this->view->cash = $cash;
		
		if ($this->getRequest()->isPost()) {
			$type = $this->getRequest()->getParam('type', 0);
			$db = $cashModel->getAdapter();
			$where = $db->quoteInto('crash_id = ?', $id);			
			$cashModel->update(array('statu'=>$type), $where);
			if ($type == 2) { //拒绝的话把钱加回去
				$accountModel = new CPyramidMemberAccount();
				$accountModel->addAmount($cash->amount, $cash->facebook_id);
			}
			$this->view->showMsg('success', 'administrator-sell-cash');	
		}
		
	}
	
	public function shareAction()
	{
		
	}
}